Thread: Assignment [pointer]

  1. #1
    Registered User
    Join Date
    Nov 2004
    Posts
    3

    Unhappy Assignment [pointer]

    I have an assignment which I asked from my friend.
    However, I do not have that much knowledge about pointer now.
    I want to ask especially about InsertTransaction.
    so please, please teach me.
    Code:
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    const int MAX_TRANS = 15;
    const char DEPOSIT = 'D';
    const char WITHDRAWAL = 'W';
    
    struct Transaction
    {
    	char transType;
    	float amount;
    };
    
    class CCheckbook
    {
    public:
    	CCheckbook(ofstream &);
    	~CCheckbook();
    	CCheckbook( const CCheckbook& otherCheckbook);
    
    	void Deposit(float);
    	void Withdrawal(float);
    	void ReadTransactions(ifstream &);
    	void InsertTransaction(char,float);
    	float GetBalance() const;
    	void PrintTransactions() const;
    	void PrintDeposits() const;
    	void PrintWithdrawals() const;
    	bool IsFull() const;
    	void SortTransactions();
    
    private:
    	Transaction * transactions[MAX_TRANS];
    	int numOfTransactions;
    	float balance;
    	ofstream & outfile;
    };
    
    
    void Print(CCheckbook);
    
    //Please add comments as necessary below including
    //putting a comment block before the function heading telling
    //what the function does and what the function returns.
    
    //Comment function parameters
    //Comment variable declarations
    //Comment code in the body of the function
    
    //All output is sent to a file. To send output to a file
    //use outfile where ever you would use cout.
    
    //outfile is a private reference member of the CCheckbook class and is initialized
    //to the passed in ofstream, out, "ofstream & out".
    
    //The initialization of outfile happens in the second line of the
    //constructor heading ":outfile(out)
    
    CCheckbook::CCheckbook(ofstream & out)//output file stream
    :outfile(out) //initialize private outfile to passed in output file stream
    {
    	outfile << "***********************" << endl;
    	outfile << "Constructor called" << endl;
    	outfile << "***********************" << endl << endl;
    
    	balance = 0;
    	numOfTransactions = 0;
    	//initialize balance and numOfTransactions
    	for (int count=0; count<MAX_TRANS; count++)
    	transactions[count] = 0;
    	//initialize every element of transactions array to NULL
    }
    
    //Answer the following questions about the destructor. 
    //Why is the destructor necessary?
    //When is a destructor called?
    
    CCheckbook::~CCheckbook()
    {
    	outfile << "***********************" << endl;
    	outfile << "Destructor called" << endl;
    	outfile << "***********************" << endl << endl;
    
    	//Deallocate memory for every dynamically allocated Transaction whose pointer is stored
    	//in the transactions array
    
    }
    
    //Answer the following questions about the copy constructor:
    //1. Why is a copy constructor needed in the CCheckbook class? 
    //2. What does a copy constructor do?
    //3. Under what 3 general conditions is a copy constructor used?
    
    CCheckbook::CCheckbook( const CCheckbook& otherCheckbook)//CCheckbook object to make copy of
    :outfile(otherCheckbook.outfile) //initialze copy's outfile to original object's output file stream
    {
    	int count;
    
    	outfile << "***********************" << endl;
    	outfile << "Copy constructor called" << endl;
    	outfile << "***********************" << endl << endl;
    	
    	//copy value of numOfTransactions and balance from otherCheckbook to numOfTransactions 
    	//and balance in this checkbook
    
    	for(count = 0; count < numOfTransactions; count++)
    	{
    			
    		//dynamically create a new Transaction using new and store
    		//the pointer to the newly allocated memory in the transactions array at the correct index
    
    		//Each pointer in the otherCheckbook's transactions array points to a Transaction
    		//struct which stores a transType and amount.
    
    		//copy the data from the Transaction struct the pointer in the otherCheckbook's 
    		//transactions array points to, to the Transaction struct the newly created 
    		//pointer in this transactions array points to.
    	}
    }
    
    
    void CCheckbook::ReadTransactions(ifstream & inputfile)
    {
    	char transType;
    	float amount;
    	int b;
    
    	while(!inputfile)
    	{
    		//Write an EOF controlled while loop that will read the data from
    		//input file
    		inputfile >> transType;
    		inputfile >> amount;
    		//Read the data into local variables
    		b = IsFull();
    		if(b=0)
    		{
    			cout << "You have exceeded max transactions allowed" << endl;
    		}
    		if(transType = 'D')
    		{
    			void Deposit(float);
    		}
    		if(transType = 'W')
    		{
    			void Withdrawal(float);
    		}
    		if(transType != 'D' && transType != 'W')
    		{
    			cout << "This type of transaction is an illegal trnasType." << endl;
    		}
    
    	//In the body of the while loop do the following:
    	//1. call a member function to see if the array is full.
    	//   If the array is full print the following error message and exit the loop 
    	//   but do not exit the program.
    	//   "You have exceeded max transactions allowed"
    	//2. Check and see what the transType is and call the appropriate member function, Deposit or Withdrawal
    	//   to process the transaction. Use the constants DEPOSIT and WITHDRAWAL for comparisons.
    	//3. If transType is not a recognized type print an error message including the illegal
    	//   transType but continue reading data from the file
    	//4. Do another read
    
    }
    
    void CCheckbook::InsertTransaction(char type,float amount)
    {
    	a = Transaction
    	//InsertTransaction will insert a pointer to a newly
    	//created Transaction into an unsorted list (array) of pointers to Transactions.
    	
    	//Dynamically create a new Transaction and store its pointer in the 
    	//transactions array at the correct index.
    
    	//Populate the new Transaction with the passed in type and amount.
    	numOfTransactions++;
    	//Increment a member variable to get ready for the next insert.
    }
    
    
    void CCheckbook::Deposit(float deposit)
    {
    	balance = balance + amount;
    	//Add the deposit to the balance and print a message to the file
    	//in the following format (balance is after deposit):
    	count << "Deposite: " << amount << "balance: " << balance << endl;
    	//"Deposit: 75.00 balance: 85.00"
    	InsertTransaction(char DEPOSIT,float amount);
    	//Call InsertTransaction with the correct arguments.
    	//The constant DEPOSIT will be one of the arguments to InsertTransaction.
    }
    
    void CCheckbook::Withdrawal(float withdrawal)
    {
    	if( balance < amount )
    	{
    		count << "Cannot make withdrawal of $" << amount
    			  << "since balance is only $" << balance << "." <<endl; 		
    	//Do not allow a withdrawal if the balance after withdrawal 
    	//will be less than 0. If the balance will be less than 0 
    	//after the withdrawal, print the following error message and exit function
    	//(The following is a sample only).
                //"Cannot make withdrawal of $60.00 since balance is only $50.00."
                //"Please make a deposit."
    	}
    	else
    	{ 
    		balance = balance - amount;
    		
    	//If the balance after the withdrawal is >=0 then process the withdrawal: 
    	//Subtract the withdrawal from the balance and print a message to the file
    	//in the following format (balance is after withdrawal):
    		count << "Withdrawal: " << amount << "balance: " << balance << endl;
    	//"Withdrawal: 75.00 balance: 10.00"
    		InsertTransaction(char WITHDRAWAL,float amount);
    	//Call InsertTransaction with the correct arguments.
    	//The constant WITHDRAWAL will be one of the arguments to InsertTransaction.
    	}
    }
    
    float CCheckbook::GetBalance() const
    {
    	return balance;
    }
    
    void CCheckbook::PrintTransactions() const
    {
    	PrintDeposits();
    	PrintWithdrawals();
    }
    
    void CCheckbook::PrintDeposits() const
    {
    	int count;
    	float sum = 0;
    
    	//iterate through the transactions array and print information
    	//about deposits in the following format. 
    	//(Include the total of all deposits made at the bottom of the column) :
    	/*
    			Deposits
    		    --------
    			100.00
    			50.50
    			224.78
    			300.00
    		    -------
    			675.28
         */
    
    }
    
    
    void CCheckbook::PrintWithdrawals() const
    {
    	int count;
    	float sum = 0;
    
    	//iterate through the transactions array and print information
    	//about withdrawals in the following format. 
    	//(Include the total of all withdrawals made at the bottom of the column) :
    	/*
    		  Withdrawals
    		   --------
    			10.25
    			102.36
    			450.12
    			50.00
    		    -------
    			612.73
    		*/
    }
    
    
    bool CCheckbook::IsFull() const
    {
    	return (numOfTransactions == MAX_TRANS);
    }
    
    // SortTransactions() - Sorts transactions array into ascending order of 
    // transaction amount using selection sort
    void  CCheckbook::SortTransactions() 
    {   
    	Transaction * pTemp ;//takes the place of temp
    
    	/*The following code has been taken from the slides
    	and will have to be modified to work with this program.
    
    	You will have to change the name of the array from data to ?
    	You will have to change length to ?
    	change temp to the above pTemp
    
    	The only other change that you will have to make is in the following line:
    	if (data[sIndx] < data[minIndx])
    
    	First change the name of the array from data to ?
    
    	Remember that stored at each array index is a pointer to
    	a Transaction not the Transaction itself.
    
    	To get to the amount stored in the pointed to Transaction you
    	will have to dereference the pointer. 
    	How do you dereference the pointer to a struct?
    
    	Modify the code below to the above requirements.
    	
    	int passCount ;
    	int sIndx ;
    	int minIndx ; 	// index of minimum so far
        
        for  ( passCount = 0 ; passCount < length - 1 ; passCount++ )
    	{
    		minIndx = passCount ;
    
    	 	// find index of smallest of data [ passCount . . length-1 ]
    		for ( sIndx = passCount + 1 ; sIndx < numOfDeposits ; sIndx++ )
         		if (data[sIndx] < data[minIndx])
    				minIndx = sIndx ;
    		temp = data[minIndx] ;		// swap 
    		data[minIndx] = data[passCount] ;
    		data[passCount] = temp ;
    	}
    	*/
    }
    int main()
    {
    	//CCheckbook checkbook;
    	ifstream inputfile;
    	ofstream outfile;
    
    	outfile.open("checkbookOutput.txt");
    
    	if(!outfile)
    	{
    		cerr << "Output file not opened";
    		return 1;
    	}
    
    	inputfile.open("transactions.txt");
    
    	if(!inputfile)
    	{
    		cerr << "Input file not opened";
    		return 1;
    	}
    
    	//All output is sent to a file.
    
    	//format the output stream so that any floating point 
    	//numbers will print to 2 places
    
    	CCheckbook CCB;
    	//declare a CCheckbook object with the correct argument
    	CCB.ReadTransactions(ifstream & inputfile);
    	//call a member function to read the input file
    	CCB.GetBalance();
    	cout << "The balance is: " << balance << endl;
    	//Call a member function to get the balance
    	//Print the balance with an appropriate message ("The balance is: ")
    	
    	//call the local Print function (below main) with the checkbook object as an argument
    	//Do not call the member function PrintTransactions here.
    
    	//The only purpose of the local Print function is to test the copy constructor.
    
    	//call a member function to sort the transactions in 
    	//ascending order of transaction amount
    
    	//call the local Print function (below main) with the checkbook object as an argument
    	//Do not call the member function PrintTransactions here.
    
    		return 0;
    }
    
    
    
    //why is the copy constructor called when Print is called?
    void Print(CCheckbook checkbook)
    {
    	//call a member function to print the transactions
    }
    /*Why is the destructor called when Print finishes executing?*//
    [FONT=Arial][COLOR=Black]
    Last edited by aotomato; 11-21-2004 at 10:10 PM. Reason: Add Tag

  2. #2
    Bob Dole for '08 B0bDole's Avatar
    Join Date
    Sep 2004
    Posts
    618
    use code tags
    Hmm

  3. #3
    Registered User
    Join Date
    Nov 2004
    Posts
    3

    Thanks

    Thank you for you advice^-^

  4. #4
    Registered User
    Join Date
    Nov 2004
    Posts
    3

    InsertTransaction

    I guess InsertTransaction function will be like this but omething wrong maybe...
    Where and how should I fix this function?
    Code:
    void CCheckbook::InsertTransaction(char type,float amount)
    {
    	Transaction t;
    	t.transType = type;
    	t.amount = amount;
    	//InsertTransaction will insert a pointer to a newly
    	//created Transaction into an unsorted list (array) of pointers to Transactions.
    	
    	int* a;
    	a = &transaction[numOfTransaction];
    	*a = t;
    	//Dynamically create a new Transaction and store its pointer in the 
    	//transactions array at the correct index.
    	
    	//Populate the new Transaction with the passed in type and amount.
    	numOfTransactions++;
    	//Increment a member variable to get ready for the next insert.
    }

  5. #5
    Registered User
    Join Date
    Mar 2003
    Location
    UK
    Posts
    170
    You're allocating memory using a local, which is allocated from the stack. When the function ends the memory on the stack is freed. So you need to allocated the memory using 'new', and when no longer needed free it somewhere within the program using 'delete'.

    Code:
    void CCheckbook::InsertTransaction(char type,float amount)
    {
         transaction[numOfTransaction] = new transaction();
         transaction[numOfTransaction]->transType = type;;
         transaction[numOfTransaction]->amount = amount;
    
         numOfTransactions++;
    }

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Menu
    By Krush in forum C Programming
    Replies: 17
    Last Post: 09-01-2009, 02:34 AM
  2. Assignment Operator, Memory and Scope
    By SevenThunders in forum C++ Programming
    Replies: 47
    Last Post: 03-31-2008, 06:22 AM
  3. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  4. Help with a pretty big C++ assignment
    By wakestudent988 in forum C++ Programming
    Replies: 1
    Last Post: 10-30-2006, 09:46 PM
  5. Replies: 1
    Last Post: 10-27-2006, 01:21 PM